package com.tlw.practice.tlw.common;

import lombok.Data;
import lombok.ToString;

/**
 * yyq
 * @param <T>
 */
@Data
@ToString
public class CommonResult<T> {
    private Integer code;
    private String message;
    private T data;

    /**
     * 成功但不带数据
     *
     * @return  SuccessResult
     */
    public static CommonResult success(){
        return success(null);
    }

    /**
     * 成功且带数据
     *
     * @return  SuccessResult
     */
    public static CommonResult success(Object data) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(ResultCode.SUCCESS.getCode());
        commonResult.setMessage(ResultCode.SUCCESS.getMessage());
        commonResult.setData(data);
        return commonResult;
    }

    /**
     * 根据 ErrorCode 创建 ErrorResult
     *
     * @param error 错误类型
     * @return      ErrorResult
     */
    public static CommonResult error(ErrorCode error){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(error.getCode());
        commonResult.setMessage(error.getMessage());
        return commonResult;
    }

    /**
     * 根据错误码和消息创建 ErrorResult
     *
     * @param code  错误码
     * @param msg   消息
     * @return      ErrorResult
     */
    public static CommonResult error(Integer code, String msg){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(code);
        commonResult.setMessage(msg);
        return commonResult;
    }

    /**
     * 根据 ResultCode 创建 CommonResult
     *
     * @param result 消息类型
     * @return      CommonResult
     */
    public static CommonResult common(ResultCode result){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(result.getCode());
        commonResult.setMessage(result.getMessage());
        return commonResult;
    }


}
